/*
 * @Author: 吴世扬 18368095041@163.com
 * @Date: 2024-08-19 16:17:03
 * @LastEditors: 吴世扬 18368095041@163.com
 * @LastEditTime: 2024-08-19 16:17:15
 * @FilePath: /vue3-uniapp/src/components/ui/FixedBottom/useCurrentPageHeightChange.ts
 */

import { ReplaySubject } from 'rxjs';

import { useBeforeRouteLeave } from '@/service/hook/useBeforeRouteLeave';

let heightSubject = new ReplaySubject<number>(1);
let height$ = heightSubject.pipe();

export const useCurrentPageHeightSubject = () => {
    const notifyHeighChange = (h: number) => {
        heightSubject.next(h);
    };

    useBeforeRouteLeave(() => {
        heightSubject.complete();
        heightSubject = new ReplaySubject<number>(1);
        height$ = heightSubject.pipe();
    });

    return {
        notifyHeighChange
    };
};

export const useCurrentPageHeightChange = (fn: (h: number) => void) => {
    const subscription = height$.subscribe({
        next(h) {
            fn(h);
        }
    });

    useBeforeRouteLeave(() => {
        subscription.unsubscribe();
    });
};
